package com.wealth.scsj.b_app.model;


import com.wealth.scsj.b_app.base.GoodsManageBase;
import com.wealth.scsj.b_app.bean.SelectAppClass1Bean;
import com.wealth.scsj.b_app.bean.SelectAppGoodsByStoreAndClassBean;

import dkmvp.net.ResponseCallback;
import dkmvp.net.ResponseHandler;
import dkmvp.net.RetryWithDelay;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 门店运营-展示一级分类
 * 门店运营-商品管理查看分类下的商品Model
 */

public class GoodsManageModel extends BaseModel implements GoodsManageBase.IModel {


    /**
     * 门店运营-展示一级分类
     * @param requestBody
     * @param callback
     * @return
     */
    @Override
    public Subscription get1Data(RequestBody requestBody, final ResponseCallback<SelectAppClass1Bean> callback) {
        Observable<SelectAppClass1Bean> observable = service.SelectAppClass1(requestBody);
        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<SelectAppClass1Bean>() {
                    @Override
                    public void call(SelectAppClass1Bean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 门店运营-商品管理查看分类下的商品
     * @param requestBody
     * @param callback
     * @return
     */
    @Override
    public Subscription get2Data(RequestBody requestBody, final ResponseCallback<SelectAppGoodsByStoreAndClassBean> callback) {
        Observable<SelectAppGoodsByStoreAndClassBean> observable = service.SelectAppGoodsByStoreAndClass(requestBody);
        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<SelectAppGoodsByStoreAndClassBean>() {
                    @Override
                    public void call(SelectAppGoodsByStoreAndClassBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }


}
